package com.codingfutrue.glacieruser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codingfutrue.glacieruser.dto.LoginDTO;
import com.codingfutrue.glacieruser.entity.Emp;
import com.codingfutrue.glacieruser.mapper.EmpMapper;
import com.codingfutrue.glacieruser.service.EmpService;
import com.codingfuture.glaciercommon.util.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Author: Ji
 * @DateTime: 2025-04-14 17:31
 * @Description:
 **/
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public Emp login(LoginDTO loginDTO) {
//        String password = loginDTO.getPassword();
//        QueryWrapper<Emp> empQueryWrapper1 = new QueryWrapper<>();
//        empQueryWrapper1.eq("login_name", loginDTO.getUsername());
//        Emp emp = empMapper.selectOne(empQueryWrapper1);
//        if (emp == null) {
//            return null;
//        }
//        String passwordInSql = emp.getPassword();
////        判断加盐加密后的密码是否正确，，需保留  待数据库密码都加密后再打开
//        if (passwordInSql != null && PasswordUtils.check(password, passwordInSql)) {
//            QueryWrapper<Emp> empQueryWrapper = new QueryWrapper<>();
//            empQueryWrapper.eq("login_name", loginDTO.getUsername())
//                    .eq("password", passwordInSql);
//            return empMapper.selectOne(empQueryWrapper);
//        }
        QueryWrapper<Emp> empQueryWrapper = new QueryWrapper<>();
        empQueryWrapper.eq("login_name", loginDTO.getUsername())
                .eq("password", loginDTO.getPassword());
        Emp emp = empMapper.selectOne(empQueryWrapper);
        return emp;
    }

    @Override
    public boolean register(LoginDTO registerDTO) {
//        String password = registerDTO.getPassword();
//        String finalPassword = PasswordUtils.encrypt(password);
//        registerDTO.setPassword(finalPassword);
//        Emp emp = registerDTO.convert2Entity();
        Emp emp = new Emp();
        emp.setLoginName(registerDTO.getUsername());
        emp.setPassword(registerDTO.getPassword());
        emp.setCreateTime(LocalDateTime.now());
        return empMapper.insert(emp) > 0;
    }

}
